package com.cx.service.impl;

import com.cx.mapper.*;
import com.cx.pojo.Carts;
import com.cx.pojo.Goods;
import com.cx.pojo.OrderGoods;
import com.cx.pojo.Orders;
import com.cx.util.OrderUtil;
import com.cx.vo.CartsInfo;
import com.cx.service.CartsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author 11597
 * @Date 2020/5/12 14:17
 */
@Service
public class CartInfoServiceImpl implements CartsInfoService {

    @Autowired
    private CartsInfoMapper cartsInfoMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    CartsMapper cartsMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<CartsInfo> selectByUserId(int userId) {
        return cartsInfoMapper.selectByUserId(userId);
    }

    /**
     * 根据用户ID和商品ID获取购物车中的对应的商品
     * @param userId
     * @param goodId
     * @return
     */
    @Override
    public CartsInfo selectByUserIdAndGoodId(int userId, int goodId) {
        return cartsInfoMapper.selectByUserIdAndGoodId(userId,goodId);
    }

    /**
     * 获取商品的价格
     * @param userid
     * @return
     */
    @Override
    public double getTotalPrice(int userid) {
        double total=0.00;
        List<CartsInfo> cartsInfoList = cartsInfoMapper.selectByUserId(userid);
        if( !cartsInfoList.isEmpty()) {
            for(CartsInfo cartsInfo : cartsInfoList) {
                total += Double.valueOf(new java.text.DecimalFormat("#.00").format(cartsInfo.getGoodPrice() * cartsInfo.getGoodNum())) ;
            }
        }
        total = Double.valueOf(new java.text.DecimalFormat("#.00").format(total));
        return total;
    }

    /**
     * 根据购物车中选中的商品生成订单
     * @param cartsList
     * @param userId
     * @param totalprice
     * @return
     */
    @Transactional
    @Override
    public int addCartsOrder(List<Carts> cartsList, Integer userId , Double totalprice) {


        //生成订单
        OrderUtil orderUtil = new OrderUtil();
        Orders orders = new Orders();

        String orderNo = orderUtil.getGeneratID();
        orders.setOrderNo(orderNo);
        orders.setUserId(userId);
        orders.setOrderStatus(-2);
        orders.setTotalMoney(BigDecimal.valueOf(totalprice));
        orders.setOrderType(1);
        orders.setIsPay(0);
        orders.setCreateTime(new Date());
        ordersMapper.insert(orders);
        //订单商品添加
        orders = ordersMapper.selectByOrderNo(orderNo);
        for (int i = 0; i < cartsList.size(); i++) {
            Carts carts = cartsList.get(i);
            OrderGoods orderGoods = new OrderGoods();
            orderGoods.setGoodId(carts.getGoodId());
            orderGoods.setOrderId(orders.getId());
            orderGoods.setGoodNum(carts.getGoodNum());
            orderGoods.setCreateTime(new Date());
            orderGoodsMapper.insert(orderGoods);
            //清除选中的购物车商品
            cartsMapper.deleteByUserIdAndGoodId(userId,carts.getGoodId());

            //减去商品的库存
            Goods goods = goodsMapper.findByGoodId(carts.getGoodId());
            goods.setGoodStock(goods.getGoodStock()-carts.getGoodNum());
            goodsMapper.updateByPrimaryKey(goods);
        }
        return orders.getId();
    }
}
